<?php
class ml_biz_feed_albumfeed{
    private $_data;
    private $_uid;
    private $_uids = array();
    private $_nicks;
    private $_content_rids = array();
    private $_goods = array();
    private $_acitve_all = array();
    private $_album_active;
    private $_active_array;
    private $_oRdsAlbum;
    const SEPARATOR_REPLY_USER = ':';
    public function __construct(){
        $this->_oRdsAlbum = new ml_model_album_rdsAlbum();
    }
    
    public function initParam($option){
        $this->_uid = $option['uid'];
        $this->_data = $option['data'];
    }
    private function _init_intra_param(){
        foreach($this->_data as $key=>$vo){
            $reply_users = $this->_oRdsAlbum->get_album_reply_users($vo['albumfeed_rid']);
            if(!is_array($reply_users)){
                $uids[] = array($vo['uid']);
            }else{
                $uids = array_merge(array($vo['uid']), $reply_users);
                $this->_data[$key]['reply_user'] = $reply_users;
            }
            $this->_uids = array_merge($this->_uids, $uids);
            if(!empty($vo['content_rid'])){
                $this->_content_rids[] = $vo['content_rid'];
            }
            $active = ml_tool_active::findAllActive($vo['content']);
            if($active){
                $this->_acitve_all = array_merge($this->_acitve_all, $active);
                $this->_data[$key]['active'] = $active;
            }
        }
        $this->_acitve_all = array_unique($this->_acitve_all);
        $this->_uids = array_unique($this->_uids);
        return true;
    }
    private function _get_active(){
        if(!count($this->_acitve_all))
            return false;
        $rs = $this->_oRdsAlbum->get_album_by_titles($this->_acitve_all);
        if(!$rs)
            return false;
        $this->_album_active = $rs;
        $this->_active_array = array_keys($this->_album_active);
        return true;
    }
    private function _get_goods(){
        if(!count($this->_content_rids))
            return false;
        
        $oContent = new ml_model_dbContent();
        $rs = $oContent->get_content_by_rids($this->_content_rids);
        if(!$rs)
            return false;
        $this->_goods = $rs;
        return true;
    }
    private function _get_nick(){
        $this->_nicks = ml_tool_nick::uids2nicks($this->_uids);
        return true;
    }
    
    private function _format_user($uid, $size=50)
    {
        return  array(
                'uid' => $uid,
                'nick' => Tool_string::un_html($this->_nicks[$uid]),
                'homeurl' => ml_tool_urlMaker::person_index($uid),
                'portrait' => ml_tool_picid::uid2portrait($uid, $size)
        );
    }
    private function _get_reply_user($uid_array){
        if(!is_array($uid_array))
            return false;
        
        foreach($uid_array as $uid){
            $user_array[] = $this->_format_user($uid, 30);
        }
        
        return $user_array;
    }
    private function _deal_data(){
        $is_active = !empty($this->_album_active);
        foreach ($this->_data as $key=>$vo){
            $this->_data[$key]['user'] = $this->_format_user($vo['uid']);
            $this->_data[$key]['reply_user'] = $this->_get_reply_user($vo['reply_user']);
            $this->_data[$key]['content'] = ml_tool_formatContent::format_content($vo['content']);
            if($is_active){
                $this->_data[$key]['content'] = $this->_format_content_by_active($this->_data[$key]['content'],$vo['active']);
            }
            $goods = $this->_goods[$vo['content_rid']];
            if($goods){
                $goods['picurl'] = ml_tool_picid::pid2url($goods['pic_id'],ML_USER_PICSIZE_FEED,ML_PICTYPE_CONTENT);
                $goods['goods_url'] = ml_tool_urlMaker::show_goods($vo['content_rid'], 'lb.asep');
                $this->_data[$key]['goods'] = $goods;
            }
            if(!empty($vo['gd_info'])){
                $this->_data[$key]['gd_info']['picurl'] = ml_tool_picid::pid2url($vo['gd_info']['gd_pic_id'],ML_USER_PICSIZE_FEED,ML_PICTYPE_CONTENT);
                $this->_data[$key]['gd_info']['picurl_big']=ml_tool_picid::pid2url($vo['gd_info']['gd_pic_id'],ML_USER_PICSIZE_SHOW,ML_PICTYPE_CONTENT);
            }
            if(!empty($this->_uid) && ($this->_uid == $vo['uid']))
                $this->_data[$key]['is_del'] = true;
            else
                $this->_data[$key]['is_del'] = false;
                
        }
        return true;
    }
    public function _format_content_by_active($content, $actives){
        if(!isset($actives))
            return $content;
        $active_array = array_intersect($actives, $this->_active_array);
        if(count($active_array)>0){
            foreach ($active_array as $active){
                $album_active[$active] = $this->_album_active[$active];
            }
            $content = ml_tool_active::active2url($content, $album_active);
        }
        return $content;
    }
    
    public function parse(){
        $this->_init_intra_param();
        $this->_get_active();
        $this->_get_nick();
        $this->_get_goods();
        $this->_deal_data();
    }
    public function create_html($is_all=true){
        $this->parse();
        foreach($this->_data as $vo){
            $html[] = ml_tool_template::parseModTpl($vo, 'pub_feed::albumfeed');
        }
        if($is_all)
            return $html;
        else
            return $html[0];
    }

    

}
?>